import 'package:demo1223/pages/details_page/details_cart.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:provide/provide.dart';
import "../provide/cart_provide.dart";
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class Cartpage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('购物车'),
      ),
      body: FutureBuilder(
        future: _getcartlists(context),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            var cartlists =
                Provide.value<CartProvides>(context).cartdata.data.goodList;
            print(cartlists);
            print(cartlists.length);
            print("===============================");
            return Container(
              color: Colors.black12,
              child: EasyRefresh(
                footer: ClassicalFooter(
                  bgColor: Colors.red,
                  textColor: Colors.white,
                  noMoreText: '',
                  infoText: '加载中',
                  loadedText: '上拉加载更多',
                ),
                // child: Container(),
                child: ListView.builder(
                  itemCount: cartlists.length,
                  itemBuilder: (context, index) {
                    return Container(
                      color: Colors.white,
                      margin: EdgeInsets.only(bottom: 15.0),
                      width: ScreenUtil().setWidth(740),
                      child: Row(
                        children: <Widget>[
                          _cartcheck(context, index),
                          _cartimg(cartlists[index].coverImg),
                          _content(
                              cartlists[index].goodsName,
                              cartlists[index].goodsModel,
                              cartlists[index].goodsNo,
                              cartlists[index].cartNum,
                              double.parse(cartlists[index].price),
                              index)
                        ],
                      ),
                    );
                  },
                ),
              ),
            );
          } else {
            return Text('加载中');
          }
        },
      ),
    );
  }

  Widget _cartcheck(context, index) {
    return Container(
      child: Provide<CartProvides>(builder: (context, child, conter) {
        var selection =
            int.parse('${conter.cartdata.data.goodList[index].isChecked}');
        print(selection);
        return Column(
          children: <Widget>[
            Checkbox(
              value: selection == 1 ? true : false,
              activeColor: Colors.pink,
              onChanged: (bool val) {
                Provide.value<CartProvides>(context).getselection(index, token);
              },
            )
          ],
        );
      }),
    );
  }

  Widget _cartimg(url) {
    return Container(
      width: ScreenUtil().setWidth(200),
      height: ScreenUtil().setHeight(200),
      child: Image.network(url),
    );
  }

  Widget _content(name, model, itemnumber, numbers, price, index) {
    return Container(
      width: ScreenUtil().setWidth(450),
      child: Column(
        children: <Widget>[
          _cartname(name),
          _cartmodel(model),
          _cartsno(itemnumber),
          _price(numbers, price, index)
        ],
      ),
    );
  }

  Widget _cartname(name) {
    return Container(
        child: Column(
      children: <Widget>[
        Row(
          children: <Widget>[
            Expanded(
              child: Text(
                name,
                style: TextStyle(fontSize: ScreenUtil().setSp(24)),
              ),
            )
          ],
        )
      ],
    ));
  }

  Widget _cartmodel(model) {
    return Row(
      children: <Widget>[
        Text(
          '型号:' + model,
          style: TextStyle(
              fontSize: ScreenUtil().setSp(20), color: Colors.black45),
        )
      ],
    );
  }

  Widget _cartsno(no) {
    return Row(
      children: <Widget>[
        Text("订货号:" + no,
            style: TextStyle(
                fontSize: ScreenUtil().setSp(20), color: Colors.black45))
      ],
    );
  }

  Widget _price(number, price, index) {
    return Container(
      child: Row(
        children: <Widget>[
          Addmover(
            nums: number,
            index: index,
            price: price,
          )
        ],
      ),
    );
  }

  // Widget _money(number, price) {
  //   var nums = number * price;
  //   nums = nums.toStringAsFixed(2);
  //   print(nums);
  //   return Container(
  //     width: ScreenUtil().setWidth(200),
  //     child: Row(
  //       children: <Widget>[Text('￥${nums}(含税)')],
  //     ),
  //   );
  // }

  Future _getcartlists(BuildContext context) async {
    print('购物车数据获取中');
    SharedPreferences prers = await SharedPreferences.getInstance();
    token = prers.getString('userInfo');
    print(token);

    await Provide.value<CartProvides>(context).getCartInfo(token);
    print('购物车数据获取中');
    //  data = Provide.value<CartProvides>(context).cartdata;
    // print(data);
    return "完成加载";
  }
}

class Addmover extends StatelessWidget {
  final int nums;
  final price;
  final int index;

  Addmover({Key key, this.nums, this.price, this.index}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(right: 0),
      height: ScreenUtil().setHeight(45),
      child: Provide<CartProvides>(
        builder: (context, child, conter) {
          var numss =
              int.parse('${conter.cartdata.data.goodList[index].cartNum}');
          var prices = numss * price;
          var pricess = prices.toStringAsFixed(2);
          print(pricess);
          print('======================');
          return Row(
            children: <Widget>[
              Container(
                child: Row(
                  children: <Widget>[Text('价格￥:${pricess}')],
                ),
              ),
              Container(
                decoration: BoxDecoration(
                    border: Border.all(width: 1, color: Colors.black45)),
                child: Row(
                  children: <Widget>[
                    GestureDetector(
                      child: Container(
                        margin: EdgeInsets.only(right: 15),
                        width: 30.0,
                        height: 30.0,
                        decoration: BoxDecoration(
                            border: Border(
                                right:
                                    BorderSide(width: 1, color: Colors.black))),
                        child: Icon(Icons.remove),
                        // margin: EdgeInsets.only(left: 150),
                      ),
                      onTap: () {
                        Provide.value<CartProvides>(context)
                            .removeCart(index, token);
                      },
                    ),
                    Text('${conter.cartdata.data.goodList[index].cartNum}'),
                    GestureDetector(
                      child: Container(
                        margin: EdgeInsets.only(left: 15),
                        width: 30.0,
                        height: 30.0,
                        decoration: BoxDecoration(
                            border: Border(
                                left:
                                    BorderSide(width: 1, color: Colors.black))),
                        child: Icon(Icons.add),
                        // margin: EdgeInsets.only(left: 150),
                      ),
                      onTap: () {
                        Provide.value<CartProvides>(context)
                            .addCart(index, token);
                      },
                    ),
                  ],
                ),
              ),
            ],
          );
        },
      ),
    );
  }
}
// //  SharedPreferences prers = await SharedPreferences.getInstance();
// //       token = prers.getString('userInfo');
